package com.pz.art.controller;

import com.pz.art.domain.AssoMemberUnit;
import com.pz.art.service.MemberService;
import com.pz.art.domain.AssoMember;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @params
 * @note 会员控制器
 * @date
 * @author lw
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController {


    @Autowired
    private MemberService memberService;

    /**
     * 获取单位会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:unitlist')")
    @GetMapping("/unitlist")
    public TableDataInfo unitlist(AssoMember member)
    {
        startPage();
        List<AssoMember> list = memberService.selectAllMember(member);
        return getDataTable(list);
    }

    /**
     * 获取普通会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:normalList')")
    @GetMapping("/normalList")
    public TableDataInfo normalList(AssoMember member)
    {
        startPage();
        List<AssoMember> list = memberService.selectNormalAllMember(member);
        return getDataTable(list);
    }

    /**
     * 查询所有会员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssoMember assoMember)
    {
        startPage();
        List<AssoMember> list = memberService.selectAssoMemberList(assoMember);
        return getDataTable(list);
    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(memberService.selectAssoMemberById(id));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssoMember assoMember)
    {
        return toAjax(memberService.insertAssoMember(assoMember));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('member:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssoMember assoMember)
    {
        return toAjax(memberService.updateAssoMember(assoMember));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('system:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(memberService.deleteAssoMemberByIds(ids));
    }


    @Log(title = "普通会员管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('member:normalexport')")
    @PostMapping("/normalexport")
    public void normalexport(HttpServletResponse response, AssoMember memberUnit)
    {
        try {
            List<AssoMember> list = memberService.selectNormalAllMember(memberUnit);
            ExcelUtil<AssoMember> util = new ExcelUtil<AssoMember>(AssoMember.class);
            util.exportExcel(response, list, "用户数据");
        }catch (Throwable e){
            System.out.println(e.getMessage());
        }

    }

    @Log(title = "普通会员管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('member:normalimportData')")
    @PostMapping("/normalimportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AssoMember> util = new ExcelUtil<AssoMember>(AssoMember.class);
        List<AssoMember> normalList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = memberService.importNormalMember(normalList, updateSupport, operName);
        return success(message);
    }

    @Log(title = "下辖会员管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('member:unitexport')")
    @PostMapping("/unitexport")
    public void unitexport(HttpServletResponse response, AssoMember memberUnit)
    {
        try {
            List<AssoMember> list = memberService.selectAllMember(memberUnit);
            ExcelUtil<AssoMember> util = new ExcelUtil<AssoMember>(AssoMember.class);
            util.exportExcel(response, list, "用户数据");
        }catch (Throwable e){
            System.out.println(e.getMessage());
        }

    }
}
